package custom

import (
	
	

	
)

const All = "$__all"

// Option represents an option that can be used to configure a custom variable.
type Option func(constant *Custom)

// ValuesMap represent a "label" to "value" map of options for a custom variable.
type ValuesMap map[string]string

func ( ValuesMap) () string {
	 := make([]string, 0, len())

	for ,  := range  {
		 = append(, )
	}

	sort.Strings()

	return strings.Join(, ",")
}

func ( ValuesMap) ( string) *sdk.StringSliceString {
	for ,  := range  {
		if  ==  {
			return &sdk.StringSliceString{Value: []string{}, Valid: true}
		}
	}

	return &sdk.StringSliceString{Value: []string{}, Valid: true}
}

// Custom represents a "custom" templated variable.
type Custom struct {
	Builder sdk.TemplateVar
	values  ValuesMap
}

// New creates a new "custom" templated variable.
func ( string,  ...Option) *Custom {
	 := &Custom{Builder: sdk.TemplateVar{
		Name:    ,
		Label:   ,
		Type:    "custom",
		Options: []sdk.Option{},
	}}

	for ,  := range  {
		()
	}

	return 
}

// Values sets the possible values for the variable.
func ( ValuesMap) Option {
	return func( *Custom) {
		for ,  := range  {
			.Builder.Options = append(.Builder.Options, sdk.Option{
				Text:  ,
				Value: ,
			})
		}

		.values = 
		.Builder.Query = .asQuery()
	}
}

// Default sets the default value of the variable.
func ( string) Option {
	return func( *Custom) {
		.Builder.Current = sdk.Current{
			Text:  .values.labelFor(),
			Value: ,
		}
	}
}

// Label sets the label of the variable.
func ( string) Option {
	return func( *Custom) {
		.Builder.Label = 
	}
}

// HideLabel ensures that this variable's label will not be displayed.
func () Option {
	return func( *Custom) {
		.Builder.Hide = 1
	}
}

// Hide ensures that the variable will not be displayed.
func () Option {
	return func( *Custom) {
		.Builder.Hide = 2
	}
}

// Multiple allows several values to be selected.
func () Option {
	return func( *Custom) {
		.Builder.Multi = true
	}
}

// IncludeAll adds an option to allow all values to be selected.
func () Option {
	return func( *Custom) {
		.Builder.IncludeAll = true
		.Builder.Options = append(.Builder.Options, sdk.Option{
			Text:  "All",
			Value: All,
		})
	}
}

// DefaultAll selects "All" values by default.
func () Option {
	return func( *Custom) {
		.Builder.Current = sdk.Current{Text: &sdk.StringSliceString{Value: []string{"All"}, Valid: true}, Value: All}
	}
}

// AllValue define the value used when selecting the "All" option.
func ( string) Option {
	return func( *Custom) {
		.Builder.AllValue = 
	}
}